<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class SharesController extends AppController {

    var $name = 'Shares';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'Catshare', 'Share'
    );
    function afterFilter() {
        //update cache
        $this->Share->cacheLastestShare();
    }

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' -Quản lý nội dung');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
                'intro LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Share.id' => 'DESC'
            ),
        );
        $data = $this->paginate('Share');

        $this->set('total', $this->params['paging']['Share']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Share->alias}.{$this->Share->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        //delete images
        $data = $this->Share->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Share.id', 'Share.images_small')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('content_image_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('content_image_absolute_path') . $image_file);
            }
        }

        if ($this->Share->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Bài viết", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Share->id = $params['cid'][0];
        } else {
            $this->Share->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        if (empty($this->data)) {
            $fields = array(
                'intro', 'title', 'content', 'id', 'images_small', 'published', 'images_large'
            );

            $this->data = $this->Share->read($fields);
            $this->set('cat_list', $this->Catshare->getListAll());
            $check_thumbnail_exist = is_file(Configure::read('content_image_absolute_path') . $this->data['Share']['images_small']);
            $this->set('check_thumbnail_exist', $check_thumbnail_exist);
            $this->set('image_url', Configure::read('content_image_url_path') . $this->data['Share']['images_small']);
            $this->set('content', $this->data['Share']['content']);
        }
    }

    function add() {
        $this->set('cat_list', $this->Catshare->getListAll());
        $this->set('title_for_layout', Configure::read('site_name') . ' - XUsers management');
    }

    function save() {
        $data = $this->data['Share'];

        //for upload thumbnail
        $this->Upload->setUploadpath(Configure::read('content_image_absolute_path'));
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));
        $row = $this->Share->findById($this->data['Share']['id']);



        if ($this->data['Share']['images_small']['name'] != '') {
            if ($this->Upload->validate($this->data['Share']['images_small'])) {
                $data['images_small'] = $this->Upload->upload($this->data['Share']['images_small']);

                //delete it
                if (is_file(Configure::read('content_image_absolute_path') . $row['Share']['images_small']) && $row['Share']['images_small'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('content_image_absolute_path') . $row['Share']['images_small']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'shares', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['images_small']);
            //delete image
            if (!empty($this->data['Share']['delete_image'])) {

                if (is_file(Configure::read('content_image_absolute_path') . $row['Share']['images_small']) && $row['Share']['images_small'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('content_image_absolute_path') . $row['Share']['images_small']);
                    $data['images_small'] = 'no_image.gif';
                }
            }
        }
        //end upload thumbnail

        $data['alias'] = $this->Alias->alias($data['title']);

        if ($this->Share->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Share->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'shares', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'shares', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->Share->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Share->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'shares', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->Share->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Share->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'shares', 'action' => 'index'));
    }

}

?>
